﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Drawing;
using System.Web.Mail;
using System.Text;

public partial class QuanTri_TrangKetQuaHocTap : System.Web.UI.Page
{
    SoLienLacDataContext db = new SoLienLacDataContext();
    clsLoadCombobox cls = new clsLoadCombobox();
    clsThaoTac thaotac = new clsThaoTac();
    clsKiemTraTrangThai clskttt = new clsKiemTraTrangThai();
    string manam;
    string malop;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["Username"] == null || Session["Username"].ToString() == "")
        {
            Response.Redirect("~/GiaoDienClient/TrangChu.aspx");
        }
        else
        {
            manam = Request.QueryString["MaNam"];
            malop = Request.QueryString["MaLop"];
            if (!IsPostBack)
            {
                cls.LoadComboboxNamHoc(drNamHoc);
                cls.LoadComboKhoi(drKhoiHoc);
                cls.LoadComboboxLopHoc(drLopHoc, int.Parse(drNamHoc.SelectedValue.ToString()));
                if (manam != "" && malop != "" && manam != null && malop != null)

                    drNamHoc.SelectedValue = manam;
                drLopHoc.SelectedValue = malop;
                TaoTabLopDong(int.Parse(drKhoiHoc.SelectedValue.ToString()));
                HienThi();


            }
        }
    }
    DataTable dtk;
    DataTable dtk1;
    void TaoTabLopDong(int makhoi)
    {
        tabLop.Tabs.Clear();
        var c = from p in db.LopHocs where p.MaKhoi == makhoi select new { p.MaLop, p.TenLop };
        if (c.Count() != 0)
        {
            foreach (var con in c)
            {
                //int i = 0;
                AjaxControlToolkit.TabPanel tabcon = new AjaxControlToolkit.TabPanel();
                tabcon.HeaderText = con.TenLop;
                tabcon.ID = "tab" + con.MaLop.ToString();

                tabLop.Tabs.Add(tabcon);
            }
        }
        // tabLop.ActiveTabIndex = 0;
    }
    protected void drNamHoc_SelectedIndexChanged(object sender, EventArgs e)
    {
        cls.LoadComboboxLopHoc(drLopHoc, int.Parse(drNamHoc.SelectedValue.ToString()));
    }
    public void HienThi()
    {
        
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int malop = int.Parse(drLopHoc.SelectedValue.ToString());
        //int maky=0;
        if (rdoKyI.Checked == true)
        {
            lblTrangThai.Text = clskttt.trangthaibangdiemcualophocky(manam, malop, 1);
            
            //if (lblTrangThai.Text != "Chưa gửi bảng điểm")
            //{
                clskttt.NgayGuiBDHK(manam, malop, 1, lblNgayGui);
                if (lblTrangThai.Text == "Đã duyệt")
                {
                    clskttt.NgayDuyetBDHK(manam, malop, 1, lblNgayDuyet);
                    btnHuy.Visible = true;
                }
                DataTable dt=DiemTongKetCacMon(manam, 1, malop);
                grvBangDiemTK.DataSource = dt;
                grvBangDiemTK.DataBind();
                thaotac.SoLuongXepLoaiHocLucKy(manam, malop, 1, lblGioi, lblKha, lblTrungBinh, lblYe1, lblKem);
            //}
        }
        if (rdoKyII.Checked == true)
        {
            lblTrangThai.Text = clskttt.trangthaibangdiemcualophocky(manam, malop, 1);
            
            if (lblTrangThai.Text != "Chưa gửi bảng điểm")
            {
                clskttt.NgayGuiBDHK(manam, malop, 2, lblNgayGui);
                if (lblTrangThai.Text == "Đã duyệt")
                {
                    clskttt.NgayDuyetBDHK(manam, malop, 2, lblNgayDuyet);
                    btnHuy.Visible = true;
                }
                DataTable dt = DiemTongKetCacMon(manam, 2, malop);
                grvBangDiemTK.DataSource = dt;
                grvBangDiemTK.DataBind();
                thaotac.SoLuongXepLoaiHocLucKy(manam, malop, 2, lblGioi, lblKha, lblTrungBinh, lblYe1, lblKem);
            }
        }
        if (rdoCaNam.Checked == true)
        {
            DataTable dt = DiemTKCaNam(manam, malop);
            grvBangDiemTK.DataSource = dt;
            grvBangDiemTK.DataBind();
            thaotac.SoLuongXepLoaiCaNam(manam, malop, lblGioi, lblKha, lblTrungBinh, lblYe1, lblKem);
        }
        kiemtratrangthai();
    }
    void kiemtratrangthai()
    {
        if (lblTrangThai.Text == "Đang chờ duyệt")
        {

            btnDuyet.Visible = true;
            btnSua.Visible = true;
            btnHuy.Visible = false;

        }

        // grvNhapDiem.Enabled = true;
        if (lblTrangThai.Text == "Cần sửa lại bảng kết quả")
        {

            btnDuyet.Visible = false;
            btnSua.Visible = false;
            btnHuy.Visible = false;
        }

        if (lblTrangThai.Text == "Đã duyệt")
        {

            btnDuyet.Visible = false;
            btnHuy.Visible = true;
            btnSua.Visible = false;

        }
        

        if (lblTrangThai.Text == "Chưa gửi bảng điểm")
        {

            btnDuyet.Visible = false;
            btnHuy.Visible = false;
            lblNgayGui.Text = "";
            btnSua.Visible = false;

        }
    }
    protected void btnHienThi_Click(object sender, EventArgs e)
    {

        HienThi();
    }
    public DataTable DiemTongKetCacMon(int manam, int maky, int malop)
    {
        int stt = 0;
        dtk = new DataTable();
        dtk.Columns.Add("STT", typeof(string));
        dtk.Columns.Add("MaHS", typeof(string));
        dtk.Columns.Add("TenHS", typeof(string));
        dtk.Columns.Add("Toan", typeof(string));
        dtk.Columns.Add("Ly", typeof(string));
        dtk.Columns.Add("Hoa", typeof(string));
        dtk.Columns.Add("Sinh", typeof(string));
        dtk.Columns.Add("Tin", typeof(string));
        dtk.Columns.Add("Van", typeof(string));
        dtk.Columns.Add("LichSu", typeof(string));
        dtk.Columns.Add("DiaLy", typeof(string));
        dtk.Columns.Add("NgoaiNgu", typeof(string));
        dtk.Columns.Add("GDCD", typeof(string));
        dtk.Columns.Add("CongNghe", typeof(string));
        dtk.Columns.Add("TheDuc", typeof(string));
        dtk.Columns.Add("GDQP", typeof(string));
        dtk.Columns.Add("DTB", typeof(string));
        dtk.Columns.Add("HocLuc", typeof(string));
        dtk.Columns.Add("HanhKiem", typeof(string));
        dtk.Columns.Add("NghiCP", typeof(string));
        dtk.Columns.Add("NghiKP", typeof(string));
        dtk.Columns.Add("DanhHieu", typeof(string));

        var c = from p in db.funcLayMaHSTrongBangDiem(manam, maky, malop) select p;
        if (c.Count() != 0)
        {
            foreach (var con in c)
            {
                stt = stt + 1;
                DataRow row = dtk.NewRow();
                row[0] = stt.ToString();
                row[1] = con.MaHS;
                row[2] = con.TenHS;
                var d1 = from p in db.funcTraVeDTBCuaTungMonHoc(manam, maky, 1, row[1].ToString()) select p.DTBMonHocKy;
                if (d1.Count() != 0)
                {
                    row[3] = d1.First().ToString();

                }
                else { row[3] = ""; }

                var d2 = from p in db.funcTraVeDTBCuaTungMonHoc(manam, maky, 2, con.MaHS.ToString()) select p.DTBMonHocKy;
                if (d2.Count() != 0)
                { row[4] = d2.First().ToString(); }
                else { row[4] = ""; }

                var d3 = from p in db.funcTraVeDTBCuaTungMonHoc(manam, maky, 3, con.MaHS.ToString()) select p.DTBMonHocKy;
                if (d3.Count() != 0)
                { row[5] = d3.First().ToString(); }
                else { row[5] = ""; }

                var d4 = from p in db.funcTraVeDTBCuaTungMonHoc(manam, maky, 4, con.MaHS.ToString()) select p.DTBMonHocKy;
                if (d4.Count() != 0)
                { row[6] = d4.First().ToString(); }
                else { row[6] = ""; }

                var d5 = from p in db.funcTraVeDTBCuaTungMonHoc(manam, maky, 5, con.MaHS.ToString()) select p.DTBMonHocKy;
                if (d5.Count() != 0)
                { row[7] = d5.First().ToString(); }
                else { row[7] = ""; }

                var d6 = from p in db.funcTraVeDTBCuaTungMonHoc(manam, maky, 6, con.MaHS.ToString()) select p.DTBMonHocKy;
                if (d6.Count() != 0)
                { row[8] = d6.First().ToString(); }
                else { row[8] = ""; }

                var d7 = from p in db.funcTraVeDTBCuaTungMonHoc(manam, maky, 7, con.MaHS) select p.DTBMonHocKy;
                if (d7.Count() != 0)
                { row[9] = d7.First().ToString(); }
                else { row[9] = ""; }

                var d8 = from p in db.funcTraVeDTBCuaTungMonHoc(manam, maky, 8, con.MaHS) select p.DTBMonHocKy;
                if (d8.Count() != 0)
                { row[10] = d8.First().ToString(); }
                else { row[10] = ""; }

                var d9 = from p in db.funcTraVeDTBCuaTungMonHoc(manam, maky, 9, con.MaHS) select p.DTBMonHocKy;
                if (d9.Count() != 0)
                { row[11] = d9.First().ToString(); }
                else { row[11] = ""; }

                var d10 = from p in db.funcTraVeDTBCuaTungMonHoc(manam, maky, 10, con.MaHS) select p.DTBMonHocKy;
                if (d10.Count() != 0)
                { row[12] = d10.First().ToString(); }
                else { row[12] = ""; }

                var d11 = from p in db.funcTraVeDTBCuaTungMonHoc(manam, maky, 11, con.MaHS) select p.DTBMonHocKy;
                if (d11.Count() != 0)
                { row[13] = d11.First().ToString(); }
                else { row[13] = ""; }

                var d12 = from p in db.funcTraVeDTBCuaTungMonHoc(manam, maky, 12, con.MaHS) select p.DTBMonHocKy;
                if (d12.Count() != 0)
                { row[14] = d12.First().ToString(); }
                else { row[14] = ""; }

                var d13 = from p in db.funcTraVeDTBCuaTungMonHoc(manam, maky, 13, con.MaHS) select p.DTBMonHocKy;
                if (d13.Count() != 0)
                { row[15] = d13.First().ToString(); }
                else { row[15] = ""; }

                var dtb = from p in db.funcTraVeDTBChungCuaHocKy(manam, maky, con.MaHS) select p;
                if (dtb.Count() != 0)
                {
                    if (dtb.First().DiemTongKet == null)
                    {
                        row[16] = ""; row[18] = "";
                    }
                    if (dtb.First().DiemTongKet != null)
                    {
                        row[16] = dtb.First().DiemTongKet.ToString();
                        if (dtb.First().HanhKiem == null)
                        {

                            row[18] = "";
                        }
                        if (dtb.First().HanhKiem != null)
                        { row[18] = dtb.First().HanhKiem.ToString(); }
                    }
                }
                else { row[16] = ""; row[18] = ""; }
                string dtoan1 = row[3].ToString();
                string dvan1 = row[8].ToString();
                float dtb1 = float.Parse(row[16].ToString());
                row[17] = thaotac.DanhGiaHocLuc(dtb1, dtoan1, dvan1, row);
                //db.procCapNhatHocLuc(manam, maky, con.MaHS, row[17].ToString());
                int nghicp = int.Parse(db.funcTraVeNghiCP(manam, maky, con.MaHS, 1).ToString());
                int nghikp = int.Parse(db.funcTraVeNghiCP(manam, maky, con.MaHS, 2).ToString());
                row[19] = nghicp.ToString();
                row[20] = nghikp.ToString();
                row[21] = thaotac.DanhGiaKhenThuong(row[17].ToString(), row[18].ToString());
                dtk.Rows.Add(row);

            }
            //grvBangDiemTK.DataSource = dtk;
            //grvBangDiemTK.DataBind();
        }
        return dtk;
        //
    }

    private DataTable DiemTKCaNam(int manam, int malop)
    {
        int stt = 0;
        dtk1 = new DataTable();
        dtk1.Columns.Add("STT", typeof(string));
        dtk1.Columns.Add("MaHS", typeof(string));
        dtk1.Columns.Add("TenHS", typeof(string));
        dtk1.Columns.Add("Toan", typeof(string));
        dtk1.Columns.Add("Ly", typeof(string));
        dtk1.Columns.Add("Hoa", typeof(string));
        dtk1.Columns.Add("Sinh", typeof(string));
        dtk1.Columns.Add("Tin", typeof(string));
        dtk1.Columns.Add("Van", typeof(string));
        dtk1.Columns.Add("LichSu", typeof(string));
        dtk1.Columns.Add("DiaLy", typeof(string));
        dtk1.Columns.Add("NgoaiNgu", typeof(string));
        dtk1.Columns.Add("GDCD", typeof(string));
        dtk1.Columns.Add("CongNghe", typeof(string));
        dtk1.Columns.Add("TheDuc", typeof(string));
        dtk1.Columns.Add("GDQP", typeof(string));
        dtk1.Columns.Add("DTB", typeof(string));
        dtk1.Columns.Add("HocLuc", typeof(string));
        dtk1.Columns.Add("HanhKiem", typeof(string));
        dtk1.Columns.Add("NghiCP", typeof(string));
        dtk1.Columns.Add("NghiKP", typeof(string));
        dtk1.Columns.Add("DanhHieu", typeof(string));
        var c = from p in db.funcLayMaHSTrongBangDiem(manam, 1, malop) select p;
        var c1 = from p in db.funcLayMaHSTrongBangDiem(manam, 2, malop) select p;
        if (c.Count() != 0)
        {
            if (c1.Count() != 0)
            {
                foreach (var con in c)
                {
                    stt = stt + 1;
                    float tongdiem = 0;
                    int tongheso = 0;
                    DataRow row = dtk1.NewRow();
                    row[0] = stt.ToString();
                    row[1] = con.MaHS;
                    row[2] = con.TenHS;
                    row[3] = thaotac.tmptinhDiemTBTCN(manam, 1, row[1].ToString(), ref tongdiem, ref tongheso);
                    row[4] = thaotac.tmptinhDiemTBTCN(manam, 2, row[1].ToString(), ref tongdiem, ref tongheso);
                    row[5] = thaotac.tmptinhDiemTBTCN(manam, 3, row[1].ToString(), ref tongdiem, ref tongheso);
                    row[6] = thaotac.tmptinhDiemTBTCN(manam, 4, row[1].ToString(), ref tongdiem, ref tongheso);
                    row[7] = thaotac.tmptinhDiemTBTCN(manam, 5, row[1].ToString(), ref tongdiem, ref tongheso);
                    row[8] = thaotac.tmptinhDiemTBTCN(manam, 6, row[1].ToString(), ref tongdiem, ref tongheso);
                    row[9] = thaotac.tmptinhDiemTBTCN(manam, 7, row[1].ToString(), ref tongdiem, ref tongheso);
                    row[10] = thaotac.tmptinhDiemTBTCN(manam, 8, row[1].ToString(), ref tongdiem, ref tongheso);
                    row[11] = thaotac.tmptinhDiemTBTCN(manam, 9, row[1].ToString(), ref tongdiem, ref tongheso);
                    row[12] = thaotac.tmptinhDiemTBTCN(manam, 10, row[1].ToString(), ref tongdiem, ref tongheso);
                    row[13] = thaotac.tmptinhDiemTBTCN(manam, 11, row[1].ToString(), ref tongdiem, ref tongheso);
                    row[14] = thaotac.tmptinhDiemTBTCN(manam, 12, row[1].ToString(), ref tongdiem, ref tongheso);
                    row[15] = thaotac.tmptinhDiemTBTCN(manam, 13, row[1].ToString(), ref tongdiem, ref tongheso);
                    string dtb1 = thaotac.LamTronDiem((tongdiem / tongheso).ToString());
                    row[16] = dtb1;
                    var hk = from p in db.funcTraVeDTBChungCuaHocKy(manam, 2, con.MaHS) select p;
                    string dtoan1 = row[3].ToString();
                    string dvan1 = row[8].ToString();
                    float dtb = float.Parse(row[16].ToString());
                    row[17] = thaotac.DanhGiaHocLuc(dtb, dtoan1, dvan1, row);
                    int nghicp1 = int.Parse(db.funcTraVeNghiCP(manam, 1, row[1].ToString(), 1).ToString());
                    int nghicp2 = int.Parse(db.funcTraVeNghiCP(manam, 2, row[1].ToString(), 1).ToString());
                    int nghikp1 = int.Parse(db.funcTraVeNghiCP(manam, 1, row[1].ToString(), 2).ToString());
                    int nghikp2 = int.Parse(db.funcTraVeNghiCP(manam, 2, row[1].ToString(), 2).ToString());
                    row[18] = "";
                    row[19] = (nghicp1 + nghicp2).ToString();
                    row[20] = (nghikp1 + nghikp2).ToString();
                    row[21] = thaotac.DanhGiaKhenThuong(row[17].ToString(), row[18].ToString());
                    dtk1.Rows.Add(row);
                    //db.procLuuHSVaoBangCaNam(manam, con.MaHS, double.Parse(row[16].ToString()), row[17].ToString());

                }
                //grvBangDiemTK.DataSource = dtk;
                //grvBangDiemTK.DataBind();
            }
            if (c1.Count() == 0)
            {

                foreach (var con in c)
                {
                    stt = stt + 1;
                    DataRow row = dtk1.NewRow();
                    row[0] = stt.ToString();
                    row[1] = con.MaHS;
                    row[2] = con.TenHS;
                    row[3] = ""; row[4] = ""; row[5] = ""; row[6] = ""; row[7] = "";
                    row[8] = ""; row[9] = ""; row[10] = ""; row[11] = "";
                    row[12] = ""; row[13] = ""; row[14] = ""; row[15] = ""; row[16] = "";
                    row[17] = "";
                    row[18] = "";
                    row[19] = "";
                    row[20] = ""; row[21] = "";
                    dtk1.Rows.Add(row);
                }
                //grvBangDiemTK.DataSource = dtk;
                //grvBangDiemTK.DataBind();
            }
        }
        return dtk1;
        //thaotac.SoLuongXepLoaiCaNam(manam, malop, lblGioi, lblKha, lblTrungBinh, lblYe1, lblKem);
    }

    protected void grvBangDiemTK_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            for (int i = 1; i <= 14; i++)
            {
                Label lbl = (Label)e.Row.FindControl("lbl"+i.ToString());
                KiemTraDiemKem(lbl);
            }
        }
    }
    void KiemTraDiemKem(Label txt)
    {
        if (txt.Text != "")
        {
            float d = float.Parse(txt.Text);
            if (d < 3.5)
            {
                txt.ForeColor = Color.Red;
            }
        }
    }
    protected void drLopHoc_SelectedIndexChanged(object sender, EventArgs e)
    {
        HienThi();
    }
    protected void rdoKyI_CheckedChanged(object sender, EventArgs e)
    {
        HienThi();
    }
    protected void rdoKyII_CheckedChanged(object sender, EventArgs e)
    {
        HienThi();
    }
    protected void rdoCaNam_CheckedChanged(object sender, EventArgs e)
    {
        HienThi();
    }
    protected void btnGuiDiem_Click(object sender, EventArgs e)
    {
        string ky="";
        if(rdoKyI.Checked==true)
        {ky="I";}
        if(rdoKyII.Checked==true)
        { ky = "II";}
        if(rdoCaNam.Checked==true)
        {ky="Cả năm";}
        for (int i = 0; i < grvBangDiemTK.Rows.Count; i++)
        {
            Label lblMa = grvBangDiemTK.Rows[i].FindControl("lblMa") as Label;
            Label lblToan = grvBangDiemTK.Rows[i].FindControl("lbl1") as Label;
            Label lblLy = grvBangDiemTK.Rows[i].FindControl("lbl2") as Label;
            Label lblHoa = grvBangDiemTK.Rows[i].FindControl("lbl3") as Label;
            Label lblSinh = grvBangDiemTK.Rows[i].FindControl("lbl4") as Label;
            Label lblTin = grvBangDiemTK.Rows[i].FindControl("lbl5") as Label;
            Label lblVan = grvBangDiemTK.Rows[i].FindControl("lbl6") as Label;
            Label lblSu = grvBangDiemTK.Rows[i].FindControl("lbl7") as Label;
            Label lblDia = grvBangDiemTK.Rows[i].FindControl("lbl8") as Label;
            Label lblNN = grvBangDiemTK.Rows[i].FindControl("lbl9") as Label;
            Label lblGDCD = grvBangDiemTK.Rows[i].FindControl("lbl10") as Label;
            Label lblCN = grvBangDiemTK.Rows[i].FindControl("lbl11") as Label;
            Label lblTheDuc = grvBangDiemTK.Rows[i].FindControl("lbl12") as Label;
            Label lblGDQP = grvBangDiemTK.Rows[i].FindControl("lbl13") as Label;
            Label lblDTB = grvBangDiemTK.Rows[i].FindControl("lbl14") as Label;
            var c = from p in db.HocSinhs where p.MaHS == lblMa.Text select new { p.Email,p.TenHS };
            if (c.Count() != 0&&c.First().Email !=null)
            {
                SendMail(c.First().Email,drLopHoc.Text,drNamHoc.Text,ky,lblMa.Text,c.First().TenHS,lblToan.Text,lblLy.Text,lblHoa.Text,lblSinh.Text,lblTin.Text,lblVan.Text,lblSu.Text,lblDia.Text,lblNN.Text,lblGDCD.Text,lblCN.Text,lblTheDuc.Text,lblGDQP.Text,lblDTB.Text);
            }
          

        }
        //MailMessage objMail = new MailMessage();

    }
    private void SendMail(string ToEmail,string lop,string nam,string ky, string mahs, string tenhs, string dtoan, string dly, string dhoa, string dsinh, string dtin, string dvan, string dsu, string ddia, string dNN, string dgdcd, string dcn, string dtheduc, string dgdqp, string dtb)
    {
        //string UrlOfSite = Request.Url.OriginalString;
        //int p = "pageRegister.aspx".Length;
        //UrlOfSite = UrlOfSite.Substring(0, UrlOfSite.Length - p);
        string Subject = "Thông báo kết quả học tập của học sinh";
        string NoiDung = "Đây là Mail gửi đến từ website của Hệ thống sổ liên lạc điện tử trường THPT Nam Khoái Châu<br>";
        NoiDung += @"Kết quả học tập của học sinh:"+tenhs+@"Lớp học:"+lop+@"Học kỳ:"+ky+@"Năm học:"+nam+@"<br>";
        NoiDung += "<br>Toán Học:" + dtoan;
        NoiDung += "<br>Vật Lý:" + dly;
        NoiDung += "<br>Hóa Học:" + dhoa;
        NoiDung += "<br>Sinh Học:" + dsinh;
        NoiDung += "<br>Tin Học:" + dtin;
        NoiDung += "<br>Ngữ Văn:" + dvan;
        NoiDung += "<br>Lịch Sử:" + dsu;
        NoiDung += "<br>Địa Lý:" + ddia;
        NoiDung += "<br>Ngoại Ngữ:" + dNN;
        NoiDung += "<br>GDCD:" + dgdcd;
        NoiDung += "<br>Công Nghệ:" + dcn;
        NoiDung += "<br>Giáo Dục Thể Chất:" + dtheduc;
        NoiDung += "<br>Quốc Phòng:" + dgdqp;
        NoiDung += "<br>Quốc Phòng:" + dtb;
        NoiDung += "<br><br><hr>Vui lòng không trả lời Mail này!";

        string Error = "";
        thaotac.SendEmail(ToEmail, Subject, NoiDung, out Error);
        ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Điểm đã gửi thành công');", true);
    }
    private string NoiDungGuiEmail(string mahs,string tenhs,string dtoan,string dly,string dhoa,string dsinh,string dtin,string dvan,string dsu,string ddia,string dNN,string dgdcd,string dcn,string dtheduc,string dgdqp,string dtb)
    {
         string NoiDung = "";
       
        NoiDung +="<br>Kết quả học tập của học sinh:"+tenhs;
     
        return NoiDung;
    }
    protected void ckHanhKiem_CheckedChanged(object sender, EventArgs e)
    {
        if (ckHanhKiem.Checked == true)
        {
            btnDuyet.Visible = false;
            btnHuy.Visible = false;
            btnSua.Visible = true;
        }
    }
    protected void btnDuyet_Click(object sender, EventArgs e)
    {
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int malop = int.Parse(drLopHoc.SelectedValue.ToString());
        int maky = 1;
        if (rdoKyI.Checked == true)
        {
            maky = 1;
        }
        if (rdoKyII.Checked == true)
        {
            maky = 2;
        }
        if (clskttt.kiemtrabdhk(manam, malop, maky) == true)
        {
            TrangThaiBangDiemTheoKy bdhk = db.TrangThaiBangDiemTheoKies.SingleOrDefault(p => p.MaKy == maky && p.MaLop == malop && p.MaNam == manam);
            bdhk.TrangThaiChoDuyet = true;
            bdhk.TrangThaiHoanThanh = true;
            bdhk.NgayDuyet = DateTime.Now.Date;
            bdhk.DaSua = true;
            if (txtThongBao.Text != null && txtThongBao.Text != "")
            {
                bdhk.ThongBao = txtThongBao.Text;
            }
            db.SubmitChanges();
            lblTrangThai.Text = clskttt.trangthaibangdiemcualophocky(manam, malop, maky);
            clskttt.NgayGuiBDHK(manam, malop, maky, lblNgayGui);
            clskttt.NgayDuyetBDHK(manam, malop, maky, lblNgayDuyet);
        }
    }
    protected void btnSua_Click(object sender, EventArgs e)
    {
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int malop = int.Parse(drLopHoc.SelectedValue.ToString());
        int maky = 1;
        if (rdoKyI.Checked == true)
        {
            maky = 1;
        }
        if (rdoKyII.Checked == true)
        {
            maky = 2;
        }
        if (clskttt.kiemtrabdhk(manam, malop, maky) == true)
        {
            TrangThaiBangDiemTheoKy bdhk = db.TrangThaiBangDiemTheoKies.SingleOrDefault(p => p.MaKy == maky && p.MaLop == malop && p.MaNam == manam);
            bdhk.TrangThaiChoDuyet = false;
            bdhk.TrangThaiHoanThanh = false;
            if (ckHanhKiem.Checked == true)
            {
                bdhk.TrangThaiHK = false;
            }
            if (ckHanhKiem.Checked == false)
            {
                bdhk.TrangThaiHK = null;
            }
            bdhk.DaSua = false;
            db.SubmitChanges();
            lblTrangThai.Text = clskttt.trangthaibangdiemcualophocky(manam, malop, maky);
            clskttt.NgayGuiBDHK(manam, malop, maky, lblNgayGui);
            lblNgayDuyet.Text = "";
            
        }
    }
    protected void btnHuy_Click(object sender, EventArgs e)
    {
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int malop = int.Parse(drLopHoc.SelectedValue.ToString());
        int maky = 1;
        if (rdoKyI.Checked == true)
        {
            maky = 1;
        }
        if (rdoKyII.Checked == true)
        {
            maky = 2;
        }
        if (clskttt.kiemtrabdhk(manam, malop, maky) == true)
        {
            TrangThaiBangDiemTheoKy bdhk = db.TrangThaiBangDiemTheoKies.SingleOrDefault(p => p.MaKy == maky && p.MaLop == malop && p.MaNam == manam);
            bdhk.TrangThaiChoDuyet = true;
            bdhk.TrangThaiHoanThanh = false;

            bdhk.DaSua = false;
            db.SubmitChanges();
            lblTrangThai.Text = clskttt.trangthaibangdiemcualophocky(manam, malop, maky);
            clskttt.NgayGuiBDHK(manam, malop, maky, lblNgayGui);
            lblNgayDuyet.Text = "";

        }
    }
    protected void btnXuatExcel_Click(object sender, EventArgs e)
    {
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int malop = int.Parse(drLopHoc.SelectedValue.ToString());
        if (rdoKyI.Checked == true)
        {
            DataTable dt = DiemTongKetCacMon(manam, 1, malop);
            thaotac.Export1(dt, "TK " + drLopHoc.SelectedItem.Text, "BẢNG ĐIỂM TỔNG KẾT LỚP " + drLopHoc.SelectedItem.Text + "NĂM HỌC " + drNamHoc.SelectedItem.Text + "HOC KỲ 1");
        }
        if (rdoKyII.Checked == true)
        {
            DataTable dt1 = DiemTongKetCacMon(manam, 2, malop);
            thaotac.Export1(dt1, "TK " + drLopHoc.SelectedItem.Text, "BẢNG ĐIỂM TỔNG KẾT LỚP " + drLopHoc.SelectedItem.Text + "NĂM HỌC " + drNamHoc.SelectedItem.Text + "HOC KỲ 2");
        }
        if (rdoCaNam.Checked == true)
        {
            DataTable dt2 = DiemTKCaNam(manam, malop);
            thaotac.Export1(dt2, "TK " + drLopHoc.SelectedItem.Text, "BẢNG ĐIỂM TỔNG KẾT LỚP " + drLopHoc.SelectedItem.Text + "  NĂM HỌC " + drNamHoc.SelectedItem.Text + "CẢ NĂM");
        }
    }
}
